import Vue from 'vue'
import Cookies from 'js-cookie'
import 'normalize.css/normalize.css'
import './styles/element-variables.scss'
import '@/styles/index.scss'
import './assets/fonts/iconfont.css'
import './icons' // icon
import './permission' // permission control
import './utils/error-log' // error log
import Element from 'element-ui'
import App from './App'
import store from './store'
import router from './router'
import * as filters from './filters' // global filters
import VueUeditorWrap from 'vue-ueditor-wrap'
import { isToday } from './utils/index'
// 在main.js中全局引入
Vue.prototype.$isToday = isToday;
Vue.component('vue-ueditor-wrap', VueUeditorWrap)
Vue.use(Element, {
  size: Cookies.get('size') || 'mini', // set element-ui default size
})
// register global utility filters
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})
Vue.config.productionTip = false
Vue.directive('tableMove', {
  bind: function (el, binding, vnode) {
    var odiv = el; // 获取当前表格元素  
    var mouseDownAndUpTimer = null;
    var mouseOffset = 0;
    var mouseFlag = false;

    // 添加可拖动样式  
    odiv.querySelector('.el-table__body-wrapper').style.cursor = 'grab';

    odiv.onmousedown = (e) => {
      if (e.which !== 1) return;
      mouseOffset = e.clientX;
      mouseDownAndUpTimer = setTimeout(function () {
        mouseFlag = true;
      }, 1);
    };

    odiv.onmouseup = (e) => {
      if (mouseFlag) {
        mouseFlag = false;
      } else {
        clearTimeout(mouseDownAndUpTimer); // 清除延迟时间  
      }
    };

    odiv.onmouseleave = (e) => {
      mouseFlag = false;
    };

    odiv.onmousemove = (e) => {
      if (e.which !== 1 || !mouseFlag) return;
      const divData = odiv.querySelector('.el-table__body-wrapper');
      if (divData) {
        // 设置水平方向的元素的位置  
        divData.scrollLeft -= (-mouseOffset + (mouseOffset = e.clientX));
      }
    };

    // 阻止默认拖动行为  
    odiv.ondragstart = (e) => {
      e.preventDefault();
    };
  }
});
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App),
  mounted() {
    document.dispatchEvent(new Event('render-event'))
  }
})
